﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class AquacultureEditControl : YuanlinUserControl,
        IReplyConverter<AquacultureReply, GetOperation>
    {
        protected Literal ltlTile;
        protected ValuedTextBox titleValuedTextBox;//常用名
        protected ValuedTextBox aliasValuedTextBox;//别名
        protected FileUpload attachmentData;//图片
        protected ValuedTextBox latinNameValuedTextBox;//拉丁名
        protected ValuedDropDownList categoryValuedDropDownList;//植物分类
        protected CheckBoxList appCategoryCheckBoxList;//应用分类
        protected YuanlinImage yuanlinImage;
        protected IButton SaveButton;
        protected IButton BackButton;
        public Aquaculture aquaculture;

        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetAquacultureRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);

        }

        private void Bind()
        {
            SetLtlTitle();

            //植物分类
            AquacultureCategoryListRequest request = new AquacultureCategoryListRequest();
            request.PageIndex = 0;
            request.PageSize = Int32.MaxValue;
            EntityList categories = BusinessManager.GetAquacultureCategoryList(RequestHelper.BuildGetAquacultureCategoryListRequest(request));

            AquacultureCategory topCategory = new AquacultureCategory();
            topCategory.Title.Value = "<选择植物分类>";
            topCategory.EntityId.Value = -1;
            categories.Insert(0, topCategory);

            this.categoryValuedDropDownList.DataSource = categories;
            this.categoryValuedDropDownList.DataTextField = "Title";
            this.categoryValuedDropDownList.DataValueField = "EntityId";
            this.categoryValuedDropDownList.DataBind();

            //应用分类
            AquacultureAppCategoryListRequest request1 = new AquacultureAppCategoryListRequest();
            request1.PageIndex = 0;
            request1.PageSize = Int32.MaxValue;
            EntityList appCategories = BusinessManager.GetAquacultureAppCategoryList(RequestHelper.BuildGetAquacultureAppCategoryListRequest(request1));

            this.appCategoryCheckBoxList.DataSource = appCategories;
            this.appCategoryCheckBoxList.DataTextField = "Title";
            this.appCategoryCheckBoxList.DataValueField = "EntityId";
            this.appCategoryCheckBoxList.DataBind();

            if (aquaculture != null)
            {
                titleValuedTextBox.Value = aquaculture.Title.Value;
                aliasValuedTextBox.Value = aquaculture.Alias.Value;
                latinNameValuedTextBox.Value = aquaculture.LatinName.Value;
                categoryValuedDropDownList.Value = aquaculture.CategoryId.Value.ToString();
                yuanlinImage.Attachment = aquaculture;

                AquacultureAndAppCategoryRelationListRequest request2 = new AquacultureAndAppCategoryRelationListRequest();
                request2.AquacultureId = aquaculture.EntityId.Value;
                request2.CategoryType = categoryType;
                EntityList fisheryAndAppCategoryRelationList = BusinessManager.GetAquacultureAndAppCategoryRelationList(RequestHelper.BuildGetAquacultureAndAppCategoryRelationListRequest(request2));
                if (fisheryAndAppCategoryRelationList.Count == 0)
                    return;
                ListItem item = null;
                foreach (AquacultureAndAppCategoryRelation fisheryAndAppCategoryRelation in fisheryAndAppCategoryRelationList)
                {
                    item = this.appCategoryCheckBoxList.Items.FindByValue(fisheryAndAppCategoryRelation.CategoryId.Value.ToString());
                    if (item != null)
                    {
                        item.Selected = true;
                    }
                }
            }
        }
        private void SetLtlTitle()
        {
            string str = "";
            switch (categoryType)
            {
                case (int)CategoryType.Fishery:
                    {
                        str = ResourceManager.GetString("System_Applications_Yuanlin_EditFishery", "ControlPanelResources.xml");
                    }
                    break;
                case (int)CategoryType.Livestock:
                    {
                        str = ResourceManager.GetString("System_Applications_Yuanlin_EditLivestock", "ControlPanelResources.xml");
                    }
                    break;
            }
            ltlTile.Text = str;
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureListUrl(string.Empty, string.Empty, categoryType));
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            aquaculture.Title.Value = CensorWordManager.CensorWord(titleValuedTextBox.Value);
            aquaculture.Alias.Value = CensorWordManager.CensorWord(aliasValuedTextBox.Value);
            aquaculture.LatinName.Value = CensorWordManager.CensorWord(latinNameValuedTextBox.Value);
            aquaculture.CategoryId.Value = Int32.Parse(categoryValuedDropDownList.Value);

            if (SiteUrls.Instance().IsAttachmentValid(attachmentData))
            {
                SiteUrls.Instance().DeleteFile(SiteUrls.Instance().GetFile(aquaculture));
                aquaculture.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(attachmentData);
                SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(aquaculture), attachmentData);
            }
            BusinessManager.UpdateAquaculture(aquaculture);

            //水产与应用分类的关系
            AquacultureAndAppCategoryRelation relation = new AquacultureAndAppCategoryRelation();
            relation.AquacultureId.Value = aquaculture.EntityId.Value;
            relation.CategoryType.Value = categoryType;
            foreach (ListItem item in this.appCategoryCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    relation.CategoryIds += "," + Globals.ChangeType<int>(item.Value);
                }
            }
            if (!string.IsNullOrEmpty(relation.CategoryIds))
            {
                relation.CategoryIds = relation.CategoryIds.Substring(1);
                BusinessManager.UpdateAquacultureAndAppCategoryRelation(relation);
            }

            Page.Response.Redirect(SiteUrls.Instance().GetAquacultureListUrl(string.Empty, string.Empty, categoryType));
        }

        #region IReplyConverter<AquacultureReply,GetOperation> Members

        public void ConvertReply(AquacultureReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                aquaculture = reply.Entity as Aquaculture;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}
